const events=require("events");
const EventEmitter=events.EventEmitter;

function Dog(dogName,energy){
    this.dogName=dogName;
    this.energy=energy;
    EventEmitter.call(this);
    this.on("bark",()=>{
        if(this.energy>0){
            console.log(this.dogName+"is barking,energy is"+" "+this.energy)
        }else{
            process.exit();
        }
        this.energy--;
    })
}

Dog.prototype=EventEmitter.prototype

let dog=new Dog("tidy",5);
let intervalId=setInterval(()=>{
    dog.emit("bark")
},1000)
